package xml;


import javax.xml.bind.annotation.adapters.XmlAdapter;

import cuShapes.Box;
import cuShapes.Connectable;
import cuShapes.PQShape;

/**
 * Switches between a java ready {@link cuShapes.Connectable Connectable} object, and an integer form of the object needed to write to xml.
 * @author mylesc,varunr
 *
 */
public class ConnectableAdapter extends XmlAdapter<Integer, Connectable> {
	
	/**
	 * Marshals a java Connectable object into a integer which is gotten from the id property of the Connectable.
	 * @param c a Connectable object to be converted into xml saveable format.
	 * @return id an integer that corresponds to the id of c.
	 */
	public Integer marshal(Connectable c) throws Exception {
		int id = c.getShapeId();
		return id;
	}

	/**
	 * Unmarshals the integer id of the Connectable and creates a new java ready PQShape with id equalling this method's param id.
	 * @param id an integer that corresponds to a PQShape in the saved file.
	 * @return newShape a new Connectable with id set to param's id.
	 */
	public Connectable unmarshal(Integer id) throws Exception { 
		/*
		 * Box is a dummy object just for containing shapeId
		 * I am using dummy objects so that the xml file only has to store a Connectables
		 * shapeId, otherwise the entire shape's data would be saved multiple times.
		 * Once the xml file is being read back in I need a temporary place(the box dummy object) for
		 * the shapeId to be stored before I call reAddConnections on each shape in shapeList.
		 * reAddConnections cannot be called immediately because some of the shapes that may
		 * be connected have yet to be created.
		 */
		PQShape newShape = new Box();
		newShape.setShapeId(id);
		 
		return (Connectable) newShape;
	}
}
